﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetFrameworkExtensions.Enterprise.Properties;

namespace NetFrameworkExtensions.Enterprise.Security
{
  public sealed class FeatureAuthorizableOperation :
    AuthorizableOperation<FeatureAuthorizableOperation>
  {
    internal FeatureAuthorizableOperation(string entityName, string featureName, AuthorizableOperationKind kind)
      : base(featureName, kind)
    {
      if (string.IsNullOrWhiteSpace(entityName))
        throw new ArgumentNullException("entityName");

      switch (kind)
      {
        case AuthorizableOperationKind.Read:
        case AuthorizableOperationKind.Update:
          break;
        default:
          throw new ArgumentOutOfRangeException("kind");
      }

      _EntityName = entityName;
    }

    private string _EntityName;
    public override string EntityName { get { return _EntityName; } }

    protected override bool EqualsOverride(FeatureAuthorizableOperation other)
    {
      return base.EqualsOverride(other) && EntityName == other.EntityName;
    }

    public override int GetHashCode()
    {
      unchecked
      {
        return base.GetHashCode() * 37 ^ EntityName.GetHashCode();
      }
    }

    public override string ToString()
    {
      switch (Kind)
      {
        case AuthorizableOperationKind.Read:
          return FormattedResources.ReadFeature(EntityName, Name);
        case AuthorizableOperationKind.Update:
          return FormattedResources.UpdateFeature(EntityName, Name);
      }
      throw new NotSupportedException();
    }
  }
}
